<template>
  <el-card>
    <div slot="header" style="line-height: 40px">
      <h1>编辑角色用户</h1>
    </div>
    <el-form
      ref="usersForm"
      :rules="rules"
      label-width="80px"
      :model="usersForm"
    >
      <el-form-item label="ID" v-show="false">
        <el-input v-model="usersForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户账号" prop="uaccount">
        <el-input v-model="usersForm.uaccount"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="upassword">
        <el-input v-model="usersForm.upassword"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="uname">
        <el-input v-model.trim="usersForm.uname"></el-input>
      </el-form-item>
      <el-form-item label="余额" prop="wallet">
        <el-input v-model.trim="usersForm.wallet"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" prop="state">
        <el-radio-group v-model="usersForm.state">
          <el-radio :label="1">账号正常</el-radio>
          <el-radio :label="0">账号异常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户角色" prop="role">
        <el-radio-group v-model="usersForm.role">
          <el-radio :label="1">测试号</el-radio>
          <el-radio :label="2">用户</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import usersApi from '@/http/usersApi';

export default {
  data() {
    return {
      usersForm: {
        uaccount:'',
        upassword:'',
        uname:'',
        wallet:'',
        state:'',
      },
      rules: {
        uname: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        uaccount: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        upassword: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        state: [
          {
            required: true,
            message: '请选择用户状态',
            trigger: 'blur',
          },
        ],
        role: [
          {
            required: true,
            message: '请选择用户角色',
            trigger: 'blur',
          },
        ],

        wallet: [
          {
            required: true,
            message: '请输入余额',
            trigger: 'blur',
          },
        ],
        uaddress: [
          {
            required: true,
            message: '请选择用户的居住地址',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['usersForm'].validate(valid => {
        if (valid) {
          console.log('submit!', this.usersForm);
          usersApi.save(this.usersForm).then(res => {
            if (res.data.code == 200) {
              this.$router.replace('/account/users.html');
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false; //阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs['usersForm'].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        usersApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.usersForm = res.data.data;
            console.log(this.usersForm);
          }
        });
      }
    },
  },
};
</script>

<style></style>
